package com.base.cn.platform.os.service.book;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.service.CommonSetSubjectDataService;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.book.BookDao;
import com.base.cn.platform.os.entity.book.Book;
import com.base.cn.platform.os.entity.book.BookCondition;
import com.base.cn.platform.os.entity.book.count.BookSumCount;
import com.base.cn.platform.os.service.manage.op.groom.OpGroomDataService;
import com.base.cn.platform.os.service.manage.subject.SubjectService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 图书Biz
 *
 * @author s.li
 * @create 2018-05-08-14:13
 */
@Service
public class BookBiz extends BaseBiz<Book,BookDao>{
    @Autowired
    private CommonSetSubjectDataService<Book> commonSetSubjectDataService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private OpGroomDataService opGroomDataService;
    @Autowired
    private BookDao bookDao;

    /**
     * 保存图书
     * @param book 图书实体对象
     * @return 图书
     */
    public Book saveBook(Book book ){
        if(DataUtil.idIsNotNull(book.getId())){//修改
            this.updateById(book,null);
        }else{//保存
            this.save(book);
        }
        return book;
    }

    /**
     * 通过ID，查询图书对象
     * @param id 图书ID
     * @param setSubjectData 是否设置专业数据
     * @param selectAllColumn 是否是查询所有的列表
     * @return 图书对象
     */
    public Book findBookById(BigDecimal id,boolean setSubjectData,boolean selectAllColumn){
        List<Book> bookList = this.findBookListByIds(id.toString(),setSubjectData,selectAllColumn);
        if(ObjectUtils.isNotEmpty(bookList)){
            return bookList.get(0);
        }
        return null;
    }

    /**
     * 通过ID串，查询图书列表
     * @param ids 图书ID串
     * @param setSubjectData 是否设置专业数据
     * @param selectAllColumn 是否是查询所有的列表
     * @return 图书Map
     */
    public Map<BigDecimal,Book> findBookMapByIds(String ids,boolean setSubjectData,boolean selectAllColumn){
        List<Book> bookList = this.findBookListByIds(ids,setSubjectData,selectAllColumn);
        if(ObjectUtils.isNotEmpty(bookList)){
            return bookList.stream().collect(Collectors.toMap(e->e.getId(),e->e));
        }
        return null;
    }

    /**
     * 通过ID串，查询图书列表
     * @param ids 图书ID串
     * @param setSubjectData 是否设置专业数据
     * @param selectAllColumn 是否是查询所有的列表
     * @return 图书列表
     */
    public List<Book> findBookListByIds(String ids,boolean setSubjectData,boolean selectAllColumn){
        BookCondition condition = new BookCondition();
        condition.setIds(ids);
        return this.findBookList(condition,setSubjectData,selectAllColumn);
    }

    /**
     * 多条件查询图书列表
     * @param condition 查询条件
     * @param setSubjectData 是否设置专业数据
     * @param selectAllColumn 是否是查询所有的列表
     * @return 图书列表
     */
    public List<Book> findBookList(BookCondition condition,boolean setSubjectData,boolean selectAllColumn){
        List<Book> bookList = this.find(this.getWhereSql(condition),condition.getCount(),this.getBookColumnName(selectAllColumn));
        bookList = commonSetSubjectDataService.setSubjectData(subjectService,bookList,setSubjectData);
        return bookList;
    }

    /**
     * 分页查询图书
     * @param condition 查询条件
     * @param page 分页条件
     * @param setSubjectData 是否设置专业数据
     * @param selectAllColumn 是否是查询所有的列表
     * @return 图书列表
     */
    public PageInfo<Book> findBookPage(BookCondition condition, Pagination page,
                                       boolean setSubjectData,boolean selectAllColumn){
        PageInfo<Book> bookPageInfo = this.findPage(this.getWhereSql(condition),page,this.getBookColumnName(selectAllColumn));
        commonSetSubjectDataService.setSubjectData(subjectService,bookPageInfo.getList(),setSubjectData);
        return bookPageInfo;
    }

    /**
     * 修改图书状态
     * @param updateList 修改的ID列表
     * @param deleteList 删除的ID列表
     * @param status 状态
     */
    public void updateBookStatus(List<BigDecimal> updateList,List<BigDecimal> deleteList,int status){
        String updateIds = "";
        String allDeleteIds = "";
        if(status==3){//删除
            if(ObjectUtils.isNotEmpty(deleteList)){
                String deleteIds = deleteList.stream().map(e->e.toString()).collect(Collectors.joining(","));
                this.deleteWhereSql(" id in ("+deleteIds+")");
                allDeleteIds = deleteIds;
            }
        }
        if(ObjectUtils.isNotEmpty(updateList)){
            updateIds = updateList.stream().map(e->e.toString()).collect(Collectors.joining(","));
            if(status==2 || status ==3){
                if(StringUtils.isNotEmpty(allDeleteIds)){
                    allDeleteIds +=","+ updateIds;
                }else{
                    allDeleteIds = updateIds;
                }
            }
        }
        if(StringUtils.isNotEmpty(updateIds)){
            Book book = new Book();
            book.setStatus(status);
            this.updateByWhereSql(book," id in ("+updateIds+")");
        }
        if(StringUtils.isNotEmpty(allDeleteIds)){
            opGroomDataService.deleteGroomDataByDataIdAndDataType(allDeleteIds,"BOOK");
        }
    }
    //============================================

    /**
     * 查询指定列的方法
     * @param selectAllColumn
     * @return
     */
    private List<String> getBookColumnName(boolean selectAllColumn){
        if(selectAllColumn){
            return null;
        }
        List<String> bookColumnName = new ArrayList<>();
        bookColumnName.add("id");
        bookColumnName.add("bookName");
        bookColumnName.add("subjectIds");
        bookColumnName.add("rawPrice");
        bookColumnName.add("realPrice");
        bookColumnName.add("memberPrice");
        bookColumnName.add("imageJson");
        bookColumnName.add("stockNum");
        bookColumnName.add("author");
        bookColumnName.add("bookPress");
        bookColumnName.add("publishTime");
        bookColumnName.add("pageNum");
        bookColumnName.add("bookISBN");
        bookColumnName.add("status");
        bookColumnName.add("createTime");
        bookColumnName.add("updateTime");
        return bookColumnName;
    }

    /**
     * 获取查询条件SQL
     * @param condition 条件对象
     * @return 查询SQL条件
     */
    private String getWhereSql(BookCondition condition){
        StringBuilder whereSql = new StringBuilder(" status!=3");
        if(ObjectUtils.isNotEmpty(condition)){
            if(StringUtils.isNotEmpty(condition.getIds())){
                whereSql .append(" and id in (").append(condition.getIds()).append(")");
            }
            if(StringUtils.isNotEmpty(condition.getBookName())){
                whereSql .append(" and bookName like '%").append(condition.getBookName().trim()).append("%'");
            }
            if(ObjectUtils.isNotEmpty(condition.getStatus())){
                whereSql .append(" and status=").append(condition.getStatus());
            }
            if(StringUtils.isNotEmpty(condition.getSubjectIds())){
                whereSql .append( StringUtils.getSubjectSql("subjectIds",condition.getSubjectIds()));
            }
            if(condition.getOrder()>0){
                int order = condition.getOrder();
                if(order==1){
                    whereSql .append( " order by updateTime desc");
                }else if(order==3){
                    whereSql .append( " order by updateTime asc");
                }else if(order==2){
                    whereSql .append( " order by realPrice desc");
                }else if(order==4){
                    whereSql .append( " order by realPrice asc");
                }
            } else {
                whereSql .append( " order by createTime desc");
            }
        }
        return whereSql.toString();
    }

    /**
     * 查询图书总数量数据
     * @return BookSumCount
     */
    public BookSumCount queryBookSumCount(){
        BookSumCount bookSumCount = bookDao.queryBookSumCount(Book.class);
        if(bookSumCount==null){
            bookSumCount = new BookSumCount();
        }
        return bookSumCount;
    }

    /**
     * 修改图书库存
     * @param data
     */
    public  void updateStockNum( List<Map<String,Object>> data){
        bookDao.updateStockNum(data);
    }
}
